﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace yule.Web
{
    
    /// <summary>
    /// 控制器基类
    /// (执行操作的Action以Do开头)
    /// </summary>
    public class BaseController : Controller
    {
        /// <summary>
        /// 缓存容器
        /// </summary>
        protected yule.Common.Cache.ICacheStrategy ShareCache
        {
            get
            {
                return yule.Common.CacheManager.ShareCache;
            }
        }

        /// <summary>
        /// 服务容器
        /// </summary>
        protected yule.Common.WCF.ServiceContext ServiceContext
        {
            get
            {
                return yule.Common.ServiceManager.ServiceContext;
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="requestContext"></param>
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
        }

        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="requestContext"></param>
        protected override void Execute(System.Web.Routing.RequestContext requestContext)
        {
            ActionContext ac = new ActionContext(requestContext);

            yule.Util.RunTime rt = new Util.RunTime();

            base.Execute(requestContext);

            double total = rt.GetTotal();

            yule.Common.LogManager.Write(
                string.Format("控制器:{0},方法:{1}。消耗:{2}毫秒", ac.ControllerName, ac.ActionName, total), Common.Log.Level.Debug);
        }

        /// <summary>
        /// 方法执行开始
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            ActionContext ac = new ActionContext(filterContext.RequestContext);



            base.OnActionExecuting(filterContext);
        }

        /// <summary>
        /// 方法执行结束
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
        }

        /// <summary>
        /// 异常处理
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnException(ExceptionContext filterContext)
        {
            string id = this.ErrorLog(filterContext.Exception);
            //设置已处理异常
            filterContext.ExceptionHandled = true;
            filterContext.Result = this.RedirectToAction("Error", "Shared", new { id = id });
        }

        /// <summary>
        /// 记录异常
        /// </summary>
        /// <param name="error"></param>
        /// <returns></returns>
        private string ErrorLog(Exception ex)
        {
            yule.IService.Sys.IError errorSvc = this.ServiceContext.CreateService<yule.IService.Sys.IError>();
            return errorSvc.AddError() ;
        }

    }

}
